package com.lframework.xingyun.promo.impl.member;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import java.io.Serializable;
import com.lframework.starter.common.utils.Assert;
import com.lframework.xingyun.promo.entity.MarketingMemberCoupon;
import com.lframework.xingyun.promo.mappers.MarketingMemberCouponMapper;
import com.lframework.xingyun.promo.service.member.MarketingMemberCouponService;
import com.lframework.xingyun.promo.vo.member.QueryMarketingMemberCouponVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MarketingMemberCouponServiceImpl extends BaseMpServiceImpl<MarketingMemberCouponMapper, MarketingMemberCoupon> implements MarketingMemberCouponService {

    @Override
    public PageResult<MarketingMemberCoupon> queryPage(Integer pageIndex, Integer pageSize, QueryMarketingMemberCouponVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MarketingMemberCoupon> page = this.getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MarketingMemberCoupon> queryList(QueryMarketingMemberCouponVo vo) {

        return this.getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MarketingMemberCoupon.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MarketingMemberCoupon findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchAdd(String marketingMemberId, List<String> couponIds) {
        List<MarketingMemberCoupon> marketingMemberCoupons = new ArrayList<>();
        for (String couponId : couponIds) {
            MarketingMemberCoupon memberCoupon = new MarketingMemberCoupon();
            memberCoupon.setMarketingMembId(marketingMemberId);
            memberCoupon.setCouponId(couponId);
            marketingMemberCoupons.add(memberCoupon);
        }
        getThis(MarketingMemberCouponService.class).saveBatch(marketingMemberCoupons);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdate(String marketingMemberId, List<String> couponIds) {
        //
        getThis(MarketingMemberCouponService.class).remove(Wrappers.lambdaUpdate(MarketingMemberCoupon.class)
                .eq(MarketingMemberCoupon::getMarketingMembId, marketingMemberId));
        getThis(MarketingMemberCouponService.class).batchAdd(marketingMemberId, couponIds);
    }

    @CacheEvict(value = MarketingMemberCoupon.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

